using System;
using System.Collections.Generic;
using BitMango;
using BitMango.Diagnostics;
using UnityEngine;

namespace BMCommon
{
    public static class PromotionPlanner
    {
        public static PromotionPlanner.Plan CurrentPlan
        {
            get
            {
                if (PromotionPlanner.schedules == null)
                {
                    return null;
                }
                foreach (PromotionPlanner.Plan plan in PromotionPlanner.schedules)
                {
                    if (plan.type != null)
                    {
                        if (plan.type.Equals("discount") && plan.IsActive)
                        {
                            return plan;
                        }
                    }
                }
                return null;
            }
        }

        public static PromotionPlanner.Plan CurrentGift
        {
            get
            {
                if (PromotionPlanner.schedules == null)
                {
                    return null;
                }
                foreach (PromotionPlanner.Plan plan in PromotionPlanner.schedules)
                {
                    if (plan.type != null)
                    {
                        if (plan.type.Equals("gift") && plan.IsActive)
                        {
                            return plan;
                        }
                    }
                }
                return null;
            }
        }

        public static List<PromotionPlanner.Plan> CurrentEvent
        {
            get
            {
                if (PromotionPlanner.schedules == null)
                {
                    return null;
                }
                List<PromotionPlanner.Plan> list = new List<PromotionPlanner.Plan>();
                foreach (PromotionPlanner.Plan plan in PromotionPlanner.schedules)
                {
                    if (plan.type != null)
                    {
                        if (plan.type.Equals("event") && plan.IsActive)
                        {
                            list.Add(plan);
                        }
                    }
                }
                return list;
            }
        }

        public static void LoadSchedules(Action<bool> callback)
        {
            if (PromotionPlanner.schedules == null)
            {
                Platform.SafeCallback<bool>(callback, false);
            }
            else
            {
                Platform.SafeCallback<bool>(callback, true);
            }
        }

        private static PromotionPlanner.Plan[] schedules;

        private static DateTime defaultDateTime = default(DateTime);

        public class Plan
        {
            private DateTime Now
            {
                get
                {
                    if (this.useUTC)
                    {
                        return DateTime.UtcNow;
                    }
                    return DateTime.Now;
                }
            }

            private PromotionPlanner.Plan.TargetDevice CurrentDeviceType
            {
                get
                {
                    if (Context.BMPlatform.storeType == PlatformContext.StoreType.GoogleStore)
                    {
                        return PromotionPlanner.Plan.TargetDevice.Google;
                    }
                    if (Context.BMPlatform.storeType == PlatformContext.StoreType.AmazonStore)
                    {
                        return PromotionPlanner.Plan.TargetDevice.Amazon;
                    }
                    return PromotionPlanner.Plan.TargetDevice.None;
                }
            }

            public bool IsActive
            {
                get
                {
                    if (this.type.Equals("gift") && this.GiftDelivered)
                    {
                        return false;
                    }
                    if (this.user != PromotionPlanner.Plan.TargetUser.All)
                    {
                        DateTime date = Context.BMPlatform.installed.Date;
                        bool flag = date >= this.begin && date <= this.end;
                        if (this.user == PromotionPlanner.Plan.TargetUser.New && !flag)
                        {
                            return false;
                        }
                        if (this.user == PromotionPlanner.Plan.TargetUser.Existing && flag)
                        {
                            return false;
                        }
                    }
                    if (this.regions != null)
                    {
                        if (!Array.Exists<string>(this.regions, (string c) => c.Equals(PromotionPlanner.Plan.countryCode)))
                        {
                            return false;
                        }
                    }
                    PromotionPlanner.Plan.TargetDevice deviceId = this.CurrentDeviceType;
                    if (this.devices != null && deviceId != PromotionPlanner.Plan.TargetDevice.None && !Array.Exists<PromotionPlanner.Plan.TargetDevice>(this.devices, (PromotionPlanner.Plan.TargetDevice c) => c == deviceId))
                    {
                        return false;
                    }
                    if (this.begin == PromotionPlanner.defaultDateTime)
                    {
                        return true;
                    }
                    DateTime now = this.Now;
                    if (this.repeat == PromotionPlanner.Plan.Repeat.None)
                    {
                        return now >= this.begin && now <= this.end;
                    }
                    TimeSpan timeOfDay = now.TimeOfDay;
                    return timeOfDay >= this.begin.TimeOfDay && timeOfDay <= this.end.TimeOfDay;
                }
            }

            public bool IsTemporal
            {
                get
                {
                    return this.begin != PromotionPlanner.defaultDateTime;
                }
            }

            public TimeSpan EndsAfter
            {
                get
                {
                    if (this.begin == PromotionPlanner.defaultDateTime)
                    {
                        return default(TimeSpan);
                    }
                    DateTime now = this.Now;
                    if (this.repeat == PromotionPlanner.Plan.Repeat.None)
                    {
                        return this.end.Subtract(now);
                    }
                    return this.end.TimeOfDay.Subtract(now.TimeOfDay);
                }
            }

            public float GetSaveForItem(string item)
            {
                foreach (PromotionPlanner.Plan.Item item2 in this.items)
                {
                    if (item2.item == item)
                    {
                        return item2.save;
                    }
                }
                return 0f;
            }

            public bool GiftDelivered
            {
                get
                {
                    return this.type.Equals("gift") && PlayerPrefs.HasKey("PromotionPlanner." + this.name);
                }
            }

            public void DeliverGift()
            {
                foreach (PromotionPlanner.Plan.Item item in this.items)
                {
                    ShopItem shopItemByID = Context.UncleBill.GetShopItemByID(item.item);
                    Debugger.Assert(shopItemByID != null);
                    if (shopItemByID != null)
                    {
                        Wallet.Gain(shopItemByID, "gift");
                    }
                }
                PlayerPrefs.SetInt("PromotionPlanner." + this.name, 1);
            }

            public string name;

            public string type;

            public bool useUTC;

            public DateTime begin;

            public DateTime end;

            public PromotionPlanner.Plan.Repeat repeat;

            public PromotionPlanner.Plan.Item[] items;

            public string[] regions;

            public PromotionPlanner.Plan.TargetDevice[] devices;

            public string text;

            public PromotionPlanner.Plan.TargetUser user;

            private static string countryCode;

            public enum Repeat
            {
                None,
                Daily
            }

            public enum ItemTag
            {
                None,
                Value,
                Pop,
                Super
            }

            public enum TargetUser
            {
                All,
                New,
                Existing
            }

            public enum TargetDevice
            {
                None,
                iPhone,
                iPad,
                Google,
                Amazon
            }

            public class Item
            {
                public bool IsPop
                {
                    get
                    {
                        return this.tag == PromotionPlanner.Plan.ItemTag.Pop;
                    }
                }

                public bool IsValue
                {
                    get
                    {
                        return this.tag == PromotionPlanner.Plan.ItemTag.Value;
                    }
                }

                public bool IsSuper
                {
                    get
                    {
                        return this.tag == PromotionPlanner.Plan.ItemTag.Super;
                    }
                }

                public ShopItem AsShopItem
                {
                    get
                    {
                        return Context.UncleBill.GetShopItemByID(this.item);
                    }
                }

                public string item;

                public float save;

                public PromotionPlanner.Plan.ItemTag tag;
            }
        }
    }
}
